package com.tongren.framework.me.activity;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.tongren.framework.R;
import com.tongren.framework.callback.JsonCallback;
import com.tongren.framework.core.BaseActivity;
import com.tongren.framework.core.CustomApplication;
import com.tongren.framework.model.BaseCourseTable;
import com.tongren.framework.model.BaseResult;
import com.tongren.framework.model.BaseUser;
import com.tongren.framework.widgets.pull.BaseViewHolder;

import java.util.ArrayList;

import static com.tongren.framework.constants.ConstantValues.BASE_IMG_URL;
import static com.tongren.framework.constants.ConstantValues.BASE_URL;

/**
 * zhangxu
 */
public class WorkInfoActivity extends BaseActivity {

    private int page = 1;
    private boolean isInitCache = false;
    private TextView tv_work_title;
    private TextView tv_work_user;
    private TextView tv_work_date;
    private TextView tv_work_realurl;
    private Button btn_download;
    private static String fileurl = "";

    @Override
    protected void setUpTitle(int titleResId) {
        super.setUpTitle(R.string.title_work_info);
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_work_info,-1);
    }

    @Override
    protected void setUpView() {
        tv_work_title = (TextView) findViewById(R.id.tv_work_title);
        tv_work_user = (TextView) findViewById(R.id.tv_work_user);
        tv_work_date = (TextView) findViewById(R.id.tv_work_date);
        tv_work_realurl = (TextView) findViewById(R.id.tv_work_realurl);
        btn_download = (Button) findViewById(R.id.btn_download);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String desp = intent.getStringExtra("desp");
        String fileType = intent.getStringExtra("fileType");
        String createName = intent.getStringExtra("createName");
        String realUrl = intent.getStringExtra("realUrl");
        fileurl = intent.getStringExtra("url");
        String date = intent.getStringExtra("date");
        tv_work_title.setText(title);
        tv_work_user.setText("作者:"+createName);
        tv_work_date.setText("日期:"+date);
        tv_work_realurl.setText("文件:"+realUrl);

        btn_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(WorkInfoActivity.this, "xiazai", Toast.LENGTH_SHORT).show();

                //下载路径，如果路径无效了，可换成你的下载路径
                String url = BASE_IMG_URL + fileurl;
                //创建下载任务,downloadUrl就是下载链接
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                //指定下载路径和下载文件名
                request.setDestinationInExternalPublicDir("", url.substring(url.lastIndexOf("/") + 1));
                //获取下载管理器
                DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                //将下载任务加入下载队列，否则不会进行下载
                downloadManager.enqueue(request);

            }
        });

    }







    class SampleViewHolder extends BaseViewHolder {

        ImageView iv_new_img;
        TextView tv_title;
        TextView tv_date;
//        TextView tv_state;

        public SampleViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_new_title);
//            tv_date = (TextView) itemView.findViewById(R.id.tv_new_date);
//            tv_state = (TextView) itemView.findViewById(R.id.tv_state);
//            iv_new_img = (ImageView) itemView.findViewById(R.id.iv_new_img);
        }

        @Override
        public void onBindViewHolder(int position) {
//            Glide.with(iv_new_img.getContext())
//                    .load(mDataList.get(position).getPhoto())
//                    .centerCrop()
//                    .placeholder(R.color.app_primary_color)
//                    .crossFade()
//                    .into(iv_new_img);
//            tv_title.setText(mDataList.get(position).getDocumentName());
//            String date = DateHelper.timeStamp2Date(mDataList.get(position).getCreateDate(),"yyyy-MM-dd HH:mm");
//            tv_date.setText(date);
//            tv_state.setText(mDataList.get(position).getState());

        }

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(WorkInfoActivity.this, "dianji"+position, Toast.LENGTH_LONG).show();
        }

    }
}
